package com.zx.auth.block.config;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.io.IOException;

/**
 * <p>
 * description: 用于处理用于权限集合序列化问题  <br>
 * create: 2025-05-04 11:23
 * </p>
 *
 * @author zhou  xun
 */
@Configuration
public class AuthJacksonConfig {

    @Bean
    public Module grantedAuthorityModule() {
        SimpleModule module = new SimpleModule();
        module.addDeserializer(GrantedAuthority.class,
                new JsonDeserializer<>() {
                    @Override
                    public GrantedAuthority deserialize(JsonParser p, DeserializationContext ctxt)
                            throws IOException {
                        return new SimpleGrantedAuthority(p.getText());
                    }
                });
        return module;
    }
}
